package com.weichen.studentgameexchange.comment.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.weichen.studentgameexchange.comment.dao.CommentAuditLogMapper;
import com.weichen.studentgameexchange.comment.entity.Comment;
import com.weichen.studentgameexchange.comment.entity.CommentAuditLog;
import com.weichen.studentgameexchange.comment.service.ICommentAuditLogService;
import com.weichen.studentgameexchange.common.common.Result;
import com.weichen.studentgameexchange.common.enums.CommentStatus;
import com.weichen.studentgameexchange.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 惟尘
 * @since 2025-02-05
 */
@Service
public class CommentAuditLogServiceImpl extends ServiceImpl<CommentAuditLogMapper, CommentAuditLog> implements ICommentAuditLogService {

    @Autowired
    CommentAuditLogMapper commentAuditLogMapper;

    /**
     * 新增审核日志
     * @param commentAuditLog
     * @return
     */
    @Override
    public Result adminAdd(CommentAuditLog commentAuditLog) {
        CommentAuditLog one = Db.lambdaQuery(CommentAuditLog.class)
                .eq(CommentAuditLog::getCommentId, commentAuditLog.getCommentId())
                .one();
        if (one != null)
            return Result.error("审核日志已存在！");
        commentAuditLogMapper.insert(commentAuditLog);
        Db.lambdaUpdate(Comment.class)
                .eq(Comment::getCommentId,commentAuditLog.getCommentId())
                .set(Comment::getStatus,commentAuditLog.getNewStatus())
                .update();
        return Result.success("添加成功！");
    }

    /**
     * 查看评论审核信息
     *
     * @param commentId
     * @return
     */
    @Override
    public CommentAuditLog adminSelectOne(Long commentId) {
        CommentAuditLog auditLog = Db.lambdaQuery(CommentAuditLog.class)
                .eq(CommentAuditLog::getCommentId, commentId)
                .one();
        if (auditLog == null)
            throw new ServiceException("审核日志不存在！");
        Comment comment = Db.lambdaQuery(Comment.class)
                .eq(Comment::getCommentId, commentId)
                .one();
//        auditLog.setOldStatus(comment.getStatus());
        return auditLog;
    }
    /**
     * 修改审核日志
     *
     * @param commentAuditLog
     * @return
     */

    @Override
    public void adminAudite(CommentAuditLog commentAuditLog) {
        Long commentId = commentAuditLog.getCommentId();
        CommentStatus oldStatus = commentAuditLog.getOldStatus();
        CommentStatus newStatus = commentAuditLog.getNewStatus();
        Comment comment = Db.lambdaQuery(Comment.class)
                .eq(Comment::getCommentId, commentId)
                .one();
        if (comment == null)
            throw new ServiceException("评论不存在！");
        Db.lambdaUpdate(Comment.class)
                .eq(Comment::getCommentId, commentId)
                .set(Comment::getStatus, newStatus)
                .update();
        Db.lambdaUpdate(CommentAuditLog.class)
                .eq(CommentAuditLog::getCommentId,commentId)
                .set(CommentAuditLog::getOldStatus,oldStatus)
                .set(CommentAuditLog::getNewStatus,newStatus)
                .set(CommentAuditLog::getAuditComment,commentAuditLog.getAuditComment())
                .update();
    }

    /**
     * 分页查询
     * @param keyword
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<CommentAuditLog> adminPage(String keyword, Integer page, Integer size) {
        Page<CommentAuditLog> commentAuditLogPage = new Page<>(page, size);
        LambdaQueryWrapper<CommentAuditLog> commentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        commentLambdaQueryWrapper.orderByDesc(CommentAuditLog::getAuditId);
        Long key = null;
        if (!StrUtil.isBlankIfStr(keyword)) {
            try {
                key = Long.parseLong(keyword);
            } catch (NumberFormatException e){
                throw new ServiceException("字符串不是一个有效的数字！");
            }
        }
        commentLambdaQueryWrapper.eq(key!=null,CommentAuditLog::getAuditId,key);
        Page<CommentAuditLog> selectPage = commentAuditLogMapper.selectPage(commentAuditLogPage, commentLambdaQueryWrapper);
        return selectPage;
    }

    /**
     * 修改审核状态
     * @param commentAuditLog
     */
    @Override
    public void adminUpadte(CommentAuditLog commentAuditLog) {
        CommentStatus newStatus = commentAuditLog.getNewStatus();
        String auditComment = commentAuditLog.getAuditComment();
        Long commentId = commentAuditLog.getCommentId();
        Db.lambdaUpdate(Comment.class)
                .eq(Comment::getCommentId,commentId)
                .set(Comment::getStatus,newStatus)
                .update();
        Db.lambdaUpdate(CommentAuditLog.class)
                .eq(CommentAuditLog::getAuditId,commentAuditLog.getAuditId())
                .set(CommentAuditLog::getOldStatus,newStatus)
                .set(CommentAuditLog::getNewStatus,newStatus)
                .set(CommentAuditLog::getAuditComment,auditComment)
                .update();
    }


}
